import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/signIn/index.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/index' },
  {
    path: '/index',
    name: 'index',
    component: index
  },
  {
    path: '/signIn',
    name: '注册',
    component: () => import('@/views/signIn/signIn.vue')
  },
  {
    path: '/home',
    name: '首页',
    component: () => import('@/views/index.vue')
  },
  {
    path: '/merchant',
    name: '商户列表',
    component: () => import('@/views/Merchant/index.vue')
  },
  {
    path: '/merchantXq',
    name: '商户详情',
    component: () => import('@/views/Merchant/merchantXq.vue')
  },

  {
    path: '/goodsXq',
    name: '商品详情',
    component: () => import('@/views/goodsXq/index.vue')
  },

  {
    path: '/goods',
    name: '商品列表',
    component: () => import('@/views/goods/index.vue')
  },
  {
    path: '/my',
    name: '我的',
    component: () => import('@/views/my/index.vue')
  },

  {
    path: '/sc',
    name: '我的',
    component: () => import('@/views/my/sc.vue')
  },
  {
    path: '/search',
    name: '搜索',
    component: () => import('@/views/search/index.vue')
  },
  {
    path: '/map',
    name: '地图导航',
    component: () => import('@/views/map/index.vue')
  },
  {
    path: '/code',
    name: '微信公众号网页授权',
    component: () => import('@/views/code/index.vue')
  },
  {
    path: '/code1',
    name: '微信公众号网页授权',
    component: () => import('@/views/code/index1.vue')
  },
]

const router = new VueRouter({
  // mode: 'history',
  routes,
})
router.beforeEach((to, from, next) => {
  if (to.path === '/signIn') return next()
  if (to.path === '/index') return next()
  let isLogin = localStorage.getItem('userlogin')
  if (isLogin) {
    //如果用户信息存在则往下执行。
    next()
  } else {
    //如果用户token不存在则跳转到login页面
    if (to.path === '/index') {
      next()
    } else {
      next('/index')
    }
  }
  next()
})
export default router
